<template>
  <a-modal
    title="物资合同"
    :visible="visible"
    @ok="handleOk"
    @cancel="visible=false"
    :width="1200"
  >
    <searchData :searchData="searchArr" @search="search"></searchData>
    <a-table
      :pagination="false"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange ,type:'radio'}"
      :columns="columns"
      :data-source="tableData"
      row-key="id"
    >
      <span slot="num" slot-scope="text, record, index">{{ index + 1 }}</span>
    </a-table>
    <pagination
      :total="totalSize"
      v-model="pageNo"
      :pageSize.sync="pageSize"
      @change="handlePageChange"
      @showSizeChange="showSizeChange"
    ></pagination>
  </a-modal>
</template>
<script>
import { materialsContractList } from '@/api/material/public/public'

export default {
  props: {
    // 自定义的接口函数
    customApi: {
      type: Function,
      default: null
    }
  },
  data: function() {
    return {
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      visible: false,
      tableData: [],
      columns: [
        { title: '序号', scopedSlots: { customRender: 'num' } },
        { title: '合同名称', dataIndex: 'name' },
        { title: '合同编号', dataIndex: 'code' },
        { title: '合同类别', dataIndex: 'contractTypeName' },
        { title: '合同来源', dataIndex: 'fromStatusName' }
      ],
      searchArr: [
        {
          name: '合同名称',
          value: 'name',
          type: 'input'
        }
      ],
      selectedRowKeys: [],
      selectRow: null
    }
  },
  methods: {
    open: function() {
      this.visible = true
      this.search()
    },
    handleOk: function() {
      this.visible = false
      this.$emit('getContract', this.selectRow)
    },
    list(params) {
      let searchData = Object.assign({ pageSize: this.pageSize, pageNo: this.pageNo }, params)
      searchData.collect = true
      const requestFn = this.customApi || materialsContractList
      requestFn(searchData).then(res => {
        if (res.code === 200) {
          this.tableData = res.data
          this.totalSize = res.totalSize
        }
      })
    },
    search(val) {
      this.pageNo = 1
      this.list(val)
    },
    handlePageChange(pageNo, pageSize) {
      this.pageNo = pageNo
      this.list()
    },
    showSizeChange(pageNo, pageSize) {
      this.pageNo = 1
      this.pageSize = pageSize
      this.list()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectRow = selectedRows[0]
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>
